//
//  VoiceMessage.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/11/24.
//

import SwiftUI
import AVFoundation

enum EnumPlayState {
    case stop
    case playing
}

struct VoiceMessage: View {
    let isMe: Bool
    let url: String
    @State var playState = EnumPlayState.stop
    @State var player: AVPlayer?
    var pub = NotificationCenter.default.publisher(for: .AVPlayerItemDidPlayToEndTime)

    var body: some View {
        HStack(alignment: .top,spacing: 0) {
            if !isMe {
                Arrow(isMe: isMe)
                if playState == .stop {
                    Image(systemName: "wave.3.forward")
                        .font(.system(size: 17))
                        .padding(.horizontal,12)
                        .padding(.vertical,10)
                        .padding(.leading,20)
                        .foregroundColor(.black)
                        .background(background)

                } else {
                    AnimatedWaveformView(color:.black)
                        .scaledToFit()
                        .frame(width:28,height:28)
                        .padding(.horizontal,10)
                        .padding(.vertical,8)
                        .padding(.leading,16)
                        .foregroundColor(.black)
                        .background(background)
                }
            }


            if isMe {
                if playState == .stop {
                    Image(systemName: "wave.3.backward")
                        .font(.system(size: 17))
                        .padding(.horizontal,12)
                        .padding(.vertical,10)
                        .padding(.trailing,20)
                        .foregroundColor(.black)
                        .background(background)
                } else {
                    AnimatedWaveformView()
                        .scaledToFit()
                        .frame(width:28,height:28)
                        .padding(.horizontal,10)
                        .padding(.vertical,8)
                        .padding(.trailing,16)
                        .foregroundColor(.black)
                        .background(background)
                }
                Arrow(isMe: isMe)
            }
        }.onTapGesture {
            play(url: URL(string: url)!)
        }.onReceive(pub) { output in
            self.playState = .stop
        }
    }


    func play(url:URL) {
        print("playing \(url)")

            let playerItem = AVPlayerItem(url: url)

            self.player = AVPlayer(playerItem:playerItem)
            player!.volume = 1.0
            player!.play()
        self.playState = .playing

    }

    struct Arrow: View {
        let isMe: Bool

        var body: some View {
            Path { path in
                path.move(to: .init(x: isMe ? 0 : 6, y: 14))
                path.addLine(to: .init(x: isMe ? 0 : 6, y: 26))
                path.addLine(to: .init(x: isMe ? 6 : 0, y: 26))
                path.addLine(to: .init(x: isMe ? 0 : 6, y: 14))
            }
            .fill(Color("chat_\(isMe ? "me" : "friend")_background"))
            .frame(width: 6,height:30)
        }
    }

    private var background: some View {
        RoundedRectangle(cornerRadius: 4)
            .foregroundColor(Color("chat_\(isMe ? "me":"friend")_background"))
    }
}

struct VoiceMessage_Previews: PreviewProvider {
    static var previews: some View {
        VoiceMessage(isMe: false, url:"")
    }
}
